<?php

/**
 * Author：晴天 QQ：1186258278
 * Creation：2020/4/22 13:31
 * Filename：jiuwu.php
 * 玖伍
 */

namespace lib\supply;

use lib\Hook\Hook;

class jiuwu
{
    /**
     * @param $OrderData 订单信息
     * @param $Goods 商品信息
     * @param $TypeSupply 对接货源信息
     * 提交下单信息！
     */
    public static function Submit($OrderData, $Goods, $TypeSupply)
    {
        global $conf;
        $DataPost = [
            'Api_UserName' => $TypeSupply['username'],
            'Api_UserMd5Pass' => md5($TypeSupply['password']),
            'goods_id' => $Goods['goods_id'],
            'goods_type' => $Goods['goods_type'],
            'need_num_0' => ($Goods['quantity'] * $OrderData['num']),
            'pay_type' => 1,
        ];
        $InputArray = json_decode($OrderData['input'], TRUE);

        if ($Goods['specification'] == 2 && $Goods['specification_type'] == 2) {
            $InputArray = RuleSubmitParameters(json_decode($Goods['specification_sku'], TRUE)[0], $InputArray);
        }

        $Field = explode(',', $Goods['submit_the_field']);
        $PostInput = [];
        $i = 0;
        foreach ($Field as $value) {
            $PostInput = array_merge($PostInput, [$value => $InputArray[$i]]);
            $i++;
        }

        $DataPost = array_merge($DataPost, $PostInput);

        $HookArr = [
            $TypeSupply['url'] => [
                'goods_id' => $Goods['goods_id'],
                'goods_type' => $Goods['goods_type'],
                'need_num_0' => ($Goods['quantity'] * $OrderData['num']),
                'PostInput' => $PostInput
            ],
            'Order' => $OrderData
        ];

        Hook::execute('JiuwuSubmit', $HookArr);

        $DataCurl = Api::SuppluCurl($TypeSupply['url'] . '/index.php?m=home&c=order&a=add', $DataPost);
        $DataCurlCopy = $DataCurl;
        $DataCurl = json_decode($DataCurl, TRUE);

        if (!isset($DataCurl['status'])) {
            $Msg = self::error($DataCurlCopy);
            $code = $conf['SubmitState'];
            $money = 0;
            $order = -1;
        } else {
            $Msg = $DataCurl['info'];
            $code = ($DataCurl['status'] >= 0 ? $conf['SubmitStateSuccess'] : $conf['SubmitState']);
            $money = ($DataCurl['status'] >= 0 ? $DataCurl['after_use_rmb'] : 0);
            $order = ($DataCurl['status'] >= 0 ? $DataCurl['order_id'] : -1);
        }

        $HookArr = [
            $TypeSupply['url'] => [
                'state' => $code,
                'msg' => $Msg,
                'money' => $money,
                'order' => $order,
            ],
            'Order' => $OrderData
        ];

        Hook::execute('JiuwuFinish', $HookArr);

        return [
            'code' => $code,
            'msg' => $Msg,
            'money' => $money,
            'order' => $order,
        ];
    }

    public static function error($data)
    {
        $regex4 = "/<p class=\"error\".*?>.*?<\/p>/";
        if (preg_match_all($regex4, $data, $matches)) {
            $info = str_replace('<p class="error">', null, $matches[0][0]);
            $info = str_replace('</p>', null, $info);
            return $info;
        } else {
            return '无法取出错误信息,下单失败！';
        }
    }

    /**
     * @param $id 订单号
     * @param $TypeSupply 对接货源信息
     */
    public static function Query($id, $TypeSupply)
    {
        $DataPost = [
            'Api_UserName' => $TypeSupply['username'],
            'Api_UserMd5Pass' => md5($TypeSupply['password']),
            'return_fields' => 'id,user_note,aa,bb,cc,dd,ee,need_num_0,need_num_1,need_num_2,need_num_3,start_num,end_num,now_num,order_state,login_state,start_time,end_time,add_time,order_amount,order_cardnum,tui_amount,tui_cardnum,user_id,card_id,order_pay_type,goods_id,goods_type,goods_type_title',
            'orders_id' => $id
        ];
        if (!strstr($TypeSupply['url'], 'http://') && !strstr($TypeSupply['url'], 'https://')) { #未包含http://修正
            $TypeSupply['url'] = 'http://' . $TypeSupply['url'] . '/';
        }
        $Data = Api::Curl($TypeSupply['url'] . '/index.php?m=Home&c=Order&a=query_orders_detail', $DataPost);
        $Data = json_decode($Data, true);
        if ($Data == null || $Data['status'] <> true) return [];
        $Data = $Data['rows'][0];

        switch ($Data['order_state']) {
            case 0:
            case 1:
                $order_state = '未开始';
                break;
            case 2:
                $order_state = '进行中';
                break;
            case 3:
                $order_state = '已完成';
                break;
            case 4:
                $order_state = '已退单';
                break;
            case 5:
                $order_state = '退单中';
                break;
            case 6:
                $order_state = '续费中';
                break;
            case 7:
                $order_state = '补单中';
                break;
            case 8:
                $order_state = '改密中';
                break;
            case 9:
                $order_state = '登录失败';
                break;
            default:
                $order_state = '未知状态';
                break;
        }

        switch ($Data['login_state']) {
            case 0:
                $login_state = '权限异常！';
                break;
            case 1:
                $login_state = '登录保护！';
                break;
            case 2:
                $login_state = '号码冻结！';
                break;
            case 3:
                $login_state = '密码错误！';
                break;
            default:
                $login_state = '未知异常!';
                break;
        }

        return [
            'ApiType' => 1,
            'ApiNum' => $Data['need_num_0'],
            'ApiTime' => $Data['add_time'],
            'ApiInitial' => $Data['start_num'],
            'ApiPresent' => $Data['now_num'],
            'ApiState' => $order_state,
            'ApiError' => ($Data['order_state'] == 9 ? $login_state : '无'),
        ];
    }
}
